package com.flex.view
{
	import General.FpsCounter;
	import General.Input;
	
	import com.adobe.cairngorm.view.ViewHelper;
	import com.flex.commands.ModelCommand;
	import com.flex.control.event.ModelCompareEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	import mx.core.UIComponent;
	

	public class ModelMainPanelHelper extends ViewHelper
	{
		public function ModelMainPanelHelper()
		{
		}
		
		import com.adobe.cairngorm.control.CairngormEventDispatcher;
		import com.flex.model.ModelLocatorI;
		
		import mx.controls.TextInput;
		import mx.events.DataGridEvent;
		import mx.events.DataGridEventReason;
		
		[Bindable]
		private var model:ModelLocatorI = ModelLocatorI.getInstance();
		
		
		public function mergeModelO(e:DataGridEvent):void
		{
			// 如果没有更新数据则直接返回，例如键盘ESC退出
			if (e.reason == DataGridEventReason.CANCELLED) return;
			else
			{
				// 获取编辑单元格的值
				var newData:String = TextInput(e.currentTarget.itemEditorInstance).text;
				switch (e.dataField)
				{

					case "kind":
						if (newData != e.currentTarget.selectedItem.pto)
						{
							var selectedPTO:Object = e.currentTarget.selectedItem as Object;
							selectedPTO.kind = int(newData);
							
							var evt:ModelCompareEvent = new ModelCompareEvent();
							evt.methodName = ModelCommand.METHOD_UPDATE_MODEL;
							evt.param = selectedPTO;
							CairngormEventDispatcher.getInstance().dispatchEvent(evt);	
						}
						break;
					
					case "label":
						if (newData != e.currentTarget.selectedItem.pto)
						{
							var selectedPTO:Object = e.currentTarget.selectedItem as Object;
							selectedPTO.label = newData;
							
							var evt:ModelCompareEvent = new ModelCompareEvent();
							evt.methodName = ModelCommand.METHOD_UPDATE_MODEL;
							evt.param = selectedPTO;
							CairngormEventDispatcher.getInstance().dispatchEvent(evt);	
						}
						break;
					case "description":
						if (newData != e.currentTarget.selectedItem.factor)
						{
							var selectedPTO:Object = e.currentTarget.selectedItem as Object;
							selectedPTO.description = newData;
							
							var evt:ModelCompareEvent = new ModelCompareEvent();
							evt.methodName = ModelCommand.METHOD_UPDATE_MODEL;
							evt.param = selectedPTO;
							CairngormEventDispatcher.getInstance().dispatchEvent(evt);	
						}
						break;
				}
			}
		}
		
		public function initView():void{
				
			var evt:ModelCompareEvent = new ModelCompareEvent();
			evt.methodName = ModelCommand.METHOD_GET_MODEL_LIST;		
			CairngormEventDispatcher.getInstance().dispatchEvent(evt);	
		}
		
		public function setModelList(modelList:ArrayCollection):void{
			(view.ptoDG as DataGrid).dataProvider = modelList;
		}
		
		
	}
	
}